package com.founder.android;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class MoviePlayerActivity extends Activity {
	
	private VideoView videoView;
	
	private List<File> files;
	
	private int movieIndex;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.screen_movie);
		videoView = (VideoView)findViewById(R.id.movieSurfaceView);
		
		addMediaController();
		
		videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			public void onCompletion(MediaPlayer mp) {
				movieIndex++;
				if (movieIndex >= files.size()) {
					finish();
				} else {
					playMovie(Uri.fromFile(files.get(movieIndex)));
				}
			}
		});
		
		String dir = getIntent().getStringExtra("dir");
		String path = PlayerUtil.getPath(dir);
		files = PlayerUtil.filterDirFile(path, PlayerUtil.TYPE_VIDEO);
		
		movieIndex = 0;
		if (files.size() > 0) {
			playMovie(Uri.fromFile(files.get(movieIndex)));
		} else {
			finish();
		}
	}
	
	private void addMediaController() {
		// Create media controller
		MediaController mediaController = new MediaController(this);
		mediaController.setVisibility(View.VISIBLE);
		videoView.setMediaController(mediaController);
	}
	
    private void playMovie(Uri uri) {
    	videoView.setVideoURI(uri);
		videoView.start();
    }

}
